package org.blitengine.core
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	
	import org.blitengine.camera.Camera;
	import org.blitengine.layers.Layer;
	
	public class Renderer extends Bitmap
	{
		public var scene:Scene;
		public var camera:Camera = new Camera();
		private var buffer:BitmapData;
		
		public function Renderer(width:int, height:int, scene:Scene)
		{
			super();
			bitmapData = new BitmapData(width, height, false, 0);
			buffer = bitmapData.clone();
			this.scene = scene;
		}
		
		public function render():void
		{
			camera.update();
			buffer.lock();
			
			buffer.fillRect(bitmapData.rect, 0);
			var layer:Layer;
			var n:int = scene.layers.length;
			for(var i:int=0; i<n; i++)
			{
				layer = Layer(scene.layers[i])
				layer.update(camera.x*layer.paralaxFactor, camera.y*layer.paralaxFactor);
				buffer.draw( layer.output );
			}
			
			buffer.unlock();
			bitmapData.copyPixels(buffer, buffer.rect, new Point());
		}
	}
}